package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.TbRepairplanlist;
import com.ruoyi.tb.lmt.domain.bo.TbLocomotiveBo;
import com.ruoyi.tb.lmt.domain.bo.TbRepairplanlistBo;
import com.ruoyi.tb.lmt.domain.vo.TbExcelVo;
import com.ruoyi.tb.lmt.domain.vo.TbLocomotiveVo;
import com.ruoyi.tb.lmt.domain.vo.TbRepairplanlistVo;
import com.ruoyi.tb.lmt.mapper.TbLocomotiveMapper;
import com.ruoyi.tb.lmt.mapper.TbRepairplanlistMapper;
import com.ruoyi.tb.lmt.service.ITbLocomotiveService;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbRepairplanBo;
import com.ruoyi.tb.lmt.domain.vo.TbRepairplanVo;
import com.ruoyi.tb.lmt.domain.TbRepairplan;
import com.ruoyi.tb.lmt.mapper.TbRepairplanMapper;
import com.ruoyi.tb.lmt.service.ITbRepairplanService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

import static com.ruoyi.common.helper.LoginHelper.getLoginUser;

/**
 * 检修计划Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-07
 */
@RequiredArgsConstructor
@Service
public class TbRepairplanServiceImpl implements ITbRepairplanService {

    private final TbRepairplanMapper baseMapper;

    private final TbRepairplanlistMapper tbRepairplanlistMapper;

    @Autowired
    private TbRepairplanMapper tbRepairplanMapper;

    @Autowired
    private ITbLocomotiveService iTbLocomotiveService;

    /**
     * 查询检修计划
     */
    @Override
    public TbRepairplanVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询检修计划列表
     */
    @Override
    public TableDataInfo<TbRepairplanVo> queryPageList(TbRepairplanBo bo, PageQuery pageQuery) {

        // 构建查询条件
        LambdaQueryWrapper<TbRepairplan> lqw = buildQueryWrapper(bo);

        // 在这里添加根据 planDate 降序排序
        pageQuery.setOrderByColumn("planDate");
        pageQuery.setIsAsc("desc");
        // 执行分页查询
        Page<TbRepairplanVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

        // 返回表格数据信息
        return TableDataInfo.build(result);

    }

    /**
     * 查询检修计划列表
     */
    @Override
    public List<TbRepairplanVo> queryList(TbRepairplanBo bo) {
        LambdaQueryWrapper<TbRepairplan> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbRepairplan> buildQueryWrapper(TbRepairplanBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbRepairplan> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getPlanCode()), TbRepairplan::getPlanCode, bo.getPlanCode());
        lqw.eq(StringUtils.isNotBlank(bo.getRepairPlanType()), TbRepairplan::getRepairPlanType, bo.getRepairPlanType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TbRepairplan::getStatus, bo.getStatus());
        lqw.like(StringUtils.isNotBlank(bo.getPlanName()), TbRepairplan::getPlanName, bo.getPlanName());
        lqw.between(StringUtils.isNotBlank(bo.getStart()) && StringUtils.isNotBlank(bo.getEnd()),
            TbRepairplan::getPlanDate, bo.getStart(), bo.getEnd());
        lqw.orderByDesc(TbRepairplan::getUpdateTime);
        return lqw;
    }

    /**
     * 新增检修计划
     */
    @Override
    public Boolean insertByBo(TbRepairplanBo bo) {
        TbRepairplan add = BeanUtil.toBean(bo, TbRepairplan.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改检修计划
     */
    @Override
    public Boolean updateByBo(TbRepairplanBo bo) {
        TbRepairplan update = BeanUtil.toBean(bo, TbRepairplan.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbRepairplan entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除检修计划
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean insertPlanAndListByBo(TbRepairplanBo bo) {
        TbRepairplan plan = BeanUtil.toBean(bo, TbRepairplan.class);
        boolean flag = baseMapper.insert(plan) > 0;
        List<TbRepairplanlistBo> repairplanlistBos = bo.getRepairplanlistVos();
        if (ObjectUtil.isNotNull(repairplanlistBos)) {
            for (int i = 0; i < repairplanlistBos.size(); i++) {
                TbRepairplanlist tbRepairplanlist = BeanUtil.toBean(repairplanlistBos.get(i), TbRepairplanlist.class);
                tbRepairplanlist.setPlanId(plan.getId());
                tbRepairplanlist.setRepairCode(UUID.fastUUID().toString());
                tbRepairplanlistMapper.insert(tbRepairplanlist);
            }
            for (TbRepairplanlistBo repairplanlistBo : repairplanlistBos) {
                System.out.println("修程" + repairplanlistBo.getRepairType());
            }
        }
        return flag;
    }

    @Override
    public Boolean updatePlanAndListByBo(TbRepairplanBo bo) {
        TbRepairplan update = BeanUtil.toBean(bo, TbRepairplan.class);
        List<TbRepairplanlistBo> repairplanlistBos = bo.getRepairplanlistVos();
        //删掉该计划下所有的计划明细
        tbRepairplanlistMapper.delete(Wrappers.<TbRepairplanlist>lambdaQuery().eq(TbRepairplanlist::getPlanId, update.getId()));
        if (ObjectUtil.isNotNull(repairplanlistBos)) {
            for (int i = 0; i < repairplanlistBos.size(); i++) {
                TbRepairplanlist tbRepairplanlist = BeanUtil.toBean(repairplanlistBos.get(i), TbRepairplanlist.class);
                tbRepairplanlist.setPlanId(update.getId());
                tbRepairplanlist.setRepairCode(UUID.fastUUID().toString());
                tbRepairplanlistMapper.insert(tbRepairplanlist);
            }
        }
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public TbRepairplanVo queryPlanAndListById(Long id) {
        TbRepairplanVo tbRepairplanVo = baseMapper.selectVoById(id);
        if (ObjectUtil.isNotNull(tbRepairplanVo)) {
//            List<TbRepairplanlist> tbRepairplanlists = tbRepairplanlistMapper.selectList(Wrappers.<TbRepairplanlist>lambdaQuery().eq(TbRepairplanlist::getPlanId, id));
//            tbRepairplanVo.setRepairplanlistVos(BeanUtil.copyToList(tbRepairplanlists, TbRepairplanlistVo.class));

            List<TbRepairplanlistVo> repairplanlistVos = tbRepairplanlistMapper.selectListById(id);
            tbRepairplanVo.setRepairplanlistVos(repairplanlistVos);

        }
        return tbRepairplanVo;
    }

    @Override
    public TableDataInfo<TbRepairplanVo> queryPagePlanAndList(TbRepairplanBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbRepairplan> lqw = buildQueryWrapper(bo);
        List<TbRepairplanVo> tbRepairplanVoList = baseMapper.selectVoList(lqw);
        if (ObjectUtil.isNotNull(tbRepairplanVoList)) {
            tbRepairplanVoList = tbRepairplanVoList.stream().map(e -> {
                List<TbRepairplanlist> tbRepairplanlistList = tbRepairplanlistMapper.selectList(Wrappers.<TbRepairplanlist>lambdaQuery().eq(TbRepairplanlist::getPlanId, e.getId()));
                e.setRepairplanlistVos(BeanUtil.copyToList(tbRepairplanlistList, TbRepairplanlistVo.class));
                return e;
            }).collect(Collectors.toList());
        }
        return TableDataInfo.build(tbRepairplanVoList);
    }

    @Override
    public Boolean deletePlanAndListByIds(List<Long> ids, Boolean isValid) {
        //删除计划明细
        tbRepairplanlistMapper.deleteBatchByPids(ids);
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean updatePlanAndListStatusByBo(TbRepairplanBo bo) {
        TbRepairplan update = BeanUtil.toBean(bo, TbRepairplan.class);
        List<TbRepairplanlist> tbRepairplanlistList = tbRepairplanlistMapper.selectList(Wrappers.<TbRepairplanlist>lambdaQuery().eq(TbRepairplanlist::getPlanId, update.getId()));
        if (tbRepairplanlistList.size() > 0) {
            if (update.getStatus().equals("正式计划")) {
                update.setApproveDate(new Date());
                LoginUser loginUser = getLoginUser();
                update.setApproveUser(loginUser.getUsername());
                for (int i = 0; i < tbRepairplanlistList.size(); i++) {
                    TbRepairplanlist tbRepairplanlist = tbRepairplanlistList.get(i);
                    tbRepairplanlist.setStatus("新计划");
                    TbLocomotiveBo lmtBo = new TbLocomotiveBo();
                    lmtBo.setLmtNumber(tbRepairplanlist.getLmtNumber());
                    lmtBo.setStatus("检修中");

                    iTbLocomotiveService.updateByBoLmtNumber(lmtBo);
                }
            } else if (update.getStatus().equals("已作废")) {
                for (int i = 0; i < tbRepairplanlistList.size(); i++) {
                    TbRepairplanlist tbRepairplanlist = tbRepairplanlistList.get(i);
                    tbRepairplanlist.setStatus("已作废");
                    //todo 任务变为作废
                    TbLocomotiveBo lmtBo = new TbLocomotiveBo();
                    lmtBo.setLmtNumber(tbRepairplanlist.getLmtNumber());
                    lmtBo.setStatus("运行中");
                    iTbLocomotiveService.updateByBoLmtNumber(lmtBo);
                }

            } else if (update.getStatus().equals("已完成")) {
                for (int i = 0; i < tbRepairplanlistList.size(); i++) {
                    TbRepairplanlist tbRepairplanlist = tbRepairplanlistList.get(i);
                    tbRepairplanlist.setStatus("已完成");
                    TbLocomotiveBo lmtBo = new TbLocomotiveBo();
                    lmtBo.setLmtNumber(tbRepairplanlist.getLmtNumber());
                    lmtBo.setStatus("运行中");
                    lmtBo.setRepairNum(tbRepairplanlist.getRepairNum());
                    lmtBo.setLastRepairChar(tbRepairplanlist.getRepairType());
                    if (tbRepairplanlist.getStartTime() != null) {
                        lmtBo.setNextPlanRepairDate(DateUtils.addDays(tbRepairplanlist.getStartTime(), 180));
                        lmtBo.setNjRepairDate(tbRepairplanlist.getStartTime());
                    } else {
                        lmtBo.setNextPlanRepairDate(DateUtils.addDays(DateUtils.getNowDate(), 180));
                        lmtBo.setNjRepairDate(DateUtils.getNowDate());
                    }
                    iTbLocomotiveService.updateByBoLmtNumber(lmtBo);
                }
            } else {
                throw new RuntimeException("不支持的状态：目前只支持（正式计划，已作废，已完成）");
            }

            tbRepairplanlistMapper.updateBatchById(tbRepairplanlistList);
        }
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public void export(HttpServletRequest request, HttpServletResponse response, Long id) {
        List<TbExcelVo> list = tbRepairplanMapper.selectExcelById(id);
        // 创建一个Excel工作簿
        Workbook workbook = new XSSFWorkbook();

        // 创建一个工作表(sheet)
        Sheet sheet = workbook.createSheet("检修计划");

        // 合并第一行的第一列和第二列
        CellRangeAddress mergedRegion = new CellRangeAddress(0, 1, 0, 3); // 参数分别是：起始行，结束行，起始列，结束列
        sheet.addMergedRegion(mergedRegion);

        // 创建第一行并在合并的单元格中插入数据
        Row titleRow = sheet.createRow(0);
        Cell titleCell = titleRow.createCell(0);
        String planName = list.get(0).getPlanName(); // 从列表中获取计划名称
        titleCell.setCellValue(planName);

        // 设置标题样式（可选）
        CellStyle titleStyle = workbook.createCellStyle();
        titleStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
        titleCell.setCellStyle(titleStyle);
        //设置日期格式
        CellStyle dateStyle = workbook.createCellStyle();
        CreationHelper createHelper = workbook.getCreationHelper();
        dateStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd"));


        // 遍历集合，处理数据
        try {
            for (TbExcelVo vo : list) {
                LocalDate startDate = (vo.getRepairStartDate() != null) ? vo.getRepairStartDate().toLocalDate() : null;
                LocalDate endDate = (vo.getRepairEndDate() != null) ? vo.getRepairEndDate().toLocalDate() : null;
                long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
                vo.setNeedDate(daysBetween);
                LocalDate zdfDate = (vo.getZdfRepairDate() != null) ? vo.getZdfRepairDate().toLocalDate() : null;
                if (zdfDate != null) {
                    vo.setPrompt(startDate.isBefore(zdfDate) ? "0" : "1");
                }
            }
        } catch (Exception e) {
            // 现在抛出一个新的RuntimeException
            throw new RuntimeException("请检查填写检修计划中计划明细是否有开始和结束检修日期。");
        }


        // 根据workerTeam对list进行排序
        //list.sort(Comparator.comparing(TbExcelVo::getWorkerTeam));

        // 创建表头行(第三行)
        Row headerRow = sheet.createRow(2); // 行索引从0开始，2表示第三行
        // 根据表头顺序创建单元格并设置值
        headerRow.createCell(0).setCellValue("车间");
        headerRow.createCell(1).setCellValue("工队");
        headerRow.createCell(2).setCellValue("车号");
        headerRow.createCell(3).setCellValue("车型");
        headerRow.createCell(4).setCellValue("修程");
        headerRow.createCell(5).setCellValue("天数");
        headerRow.createCell(7).setCellValue("备注");
        headerRow.createCell(8).setCellValue("长度系数");
        headerRow.createCell(9).setCellValue("开始检修时间");
        headerRow.createCell(10).setCellValue("检修结束时间");
        headerRow.createCell(11).setCellValue("提示");
        headerRow.createCell(12).setCellValue("制动部件校验有效期");
        headerRow.createCell(13).setCellValue("车辆探伤有效期");
        headerRow.createCell(15).setCellValue("检修车辆长度：");

        // 从第四行开始，遍历数据列表并填充数据
        int rowNum = 3; // 行索引从0开始
        for (TbExcelVo vo : list) {
            Row row = sheet.createRow(rowNum++);
            // 根据TbExcelVo对象的属性，创建单元格并设置值

        }

        // 查找最早和最晚日期
        LocalDate minStartDate = list.stream()
            .map(vo -> vo.getRepairStartDate().toLocalDate())
            .min(LocalDate::compareTo)
            .orElse(null);

        LocalDate maxEndDate = list.stream()
            .map(vo -> vo.getRepairEndDate().toLocalDate())
            .max(LocalDate::compareTo)
            .orElse(null);

        if (minStartDate != null && maxEndDate != null) {
            // 创建日期行
//            Row dateRow = sheet.createRow(0);
//            Row sumRow = sheet.createRow(1); // 计算每天prompt值总和的行
            int dateCellIndex = 15; // 从第16列开始
            for (LocalDate date = minStartDate; !date.isAfter(maxEndDate); date = date.plusDays(1)) {
                Cell dateCell = titleRow.createCell(dateCellIndex);
                dateCell.setCellValue(Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant()));
                dateCell.setCellStyle(dateStyle); // 假设已经创建了日期样式

                // 在总和行中先初始化每个单元格的值为0
                headerRow.createCell(dateCellIndex).setCellValue(0);

                dateCellIndex++;
            }

            // 从第四行开始填入longFactor值，并计算总和
            int rowNumber = 3; // 数据从第四行开始
            for (TbExcelVo vo : list) {
                Row row = sheet.createRow(rowNumber++);
                row.createCell(0).setCellValue(vo.getWorkerTeam());
                row.createCell(1).setCellValue(vo.getWorkGroup());
                row.createCell(2).setCellValue(vo.getLmtNumber());
                row.createCell(3).setCellValue(vo.getLmtType());
                row.createCell(4).setCellValue(vo.getRepairType());
                row.createCell(5).setCellValue(vo.getNeedDate());
                row.createCell(6).setCellValue(vo.getRemark());
                row.createCell(7).setCellValue(vo.getLongFactor());
                // 对于日期，可能需要进行格式化

                Cell startDateCell = row.createCell(8);
                startDateCell.setCellValue(vo.getRepairStartDate());
                startDateCell.setCellStyle(dateStyle);

                Cell endDateCell = row.createCell(9);
                endDateCell.setCellValue(vo.getRepairEndDate());
                endDateCell.setCellStyle(dateStyle);

                row.createCell(10).setCellValue(vo.getPrompt());

                Cell cell11 = row.createCell(11);
                cell11.setCellValue(vo.getZdfRepairDate());
                cell11.setCellStyle(dateStyle);

                Cell cell12 = row.createCell(12);
                cell12.setCellValue(vo.getTsRepairDate());
                cell12.setCellStyle(dateStyle);
                LocalDate startDate = vo.getRepairStartDate().toLocalDate();
                LocalDate endDate = vo.getRepairEndDate().toLocalDate();
                int startCellIndex = 15 + (int) ChronoUnit.DAYS.between(minStartDate, startDate);
                int endCellIndex = 15 + (int) ChronoUnit.DAYS.between(minStartDate, endDate);

                for (int cellIndex = startCellIndex; cellIndex <= endCellIndex; cellIndex++) {
                    Cell promptCell = row.createCell(cellIndex);
                    promptCell.setCellValue(vo.getLongFactor());

                    // 更新总和行的值
                    try {
                        double currentSum = headerRow.getCell(cellIndex).getNumericCellValue();
                        headerRow.getCell(cellIndex).setCellValue(currentSum + Double.parseDouble(vo.getLongFactor()));
                    } catch (Exception e) {
                        throw new IllegalArgumentException("该检修计划中，存在大机长度系数为空");
                    }

                }
            }
        }


        // 调整列宽以适应内容
        // 设置所有行高
        for (Row row : sheet) {
            row.setHeightInPoints(20);
        }

// 设置所有列宽
        for (Sheet sh : workbook) {
            for (int i = 0; i < 300; i++) {
                sh.setColumnWidth(i, 14 * 256);
            }
        }


        OutputStream os = null;

        try {
//            response.setContentType("application/vnd.ms-excel");
//            response.setHeader("Content-disposition", "attachment;filename=检修计划.xlsx");
            os = response.getOutputStream();
            workbook.write(os);
            os.flush();
            //os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public TbRepairplanVo nowList() {
        return tbRepairplanMapper.selectNowList();
    }

    @Override
    public List<TbRepairplanVo> allList() {
        return tbRepairplanMapper.selectAllList();
    }


    @Override
    public void export1(HttpServletRequest request, HttpServletResponse response, Long id) {
        List<TbExcelVo> list = tbRepairplanMapper.selectExcelById(id);
        // 创建一个Excel工作簿
        Workbook workbook = new XSSFWorkbook();

        // 创建一个工作表(sheet)
        Sheet sheet = workbook.createSheet("检修计划");


        // 创建第一行插入数据
        Row titleRow = sheet.createRow(0); // 创建第一行
        Cell titleCell = titleRow.createCell(2); // 创建第三列的单元格（索引为2）
        String planName = list.get(0).getPlanName(); // 从列表中获取计划名称
        titleCell.setCellValue(planName); // 设置单元格的值为计划名称

        // 设置标题样式（可选）
        CellStyle titleStyle = workbook.createCellStyle();
        titleStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
        titleCell.setCellStyle(titleStyle);
        //设置日期格式
        CellStyle dateStyle = workbook.createCellStyle();
        CreationHelper createHelper = workbook.getCreationHelper();
        dateStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd"));


        // 创建表头行(第4行)
        Row headerRow = sheet.createRow(3); // 行索引从0开始，3表示第4行
        // 根据表头顺序创建单元格并设置值
        headerRow.createCell(0).setCellValue("车间");
        headerRow.createCell(1).setCellValue("工队");
        headerRow.createCell(2).setCellValue("车号");
        headerRow.createCell(3).setCellValue("车型");
        headerRow.createCell(4).setCellValue("修程");
        headerRow.createCell(5).setCellValue("天数");
        headerRow.createCell(7).setCellValue("备注");
        headerRow.createCell(8).setCellValue("长度系数");
        headerRow.createCell(9).setCellValue("开始检修时间");
        headerRow.createCell(10).setCellValue("检修结束时间");
        headerRow.createCell(11).setCellValue("提示");
        headerRow.createCell(12).setCellValue("制动部件校验有效期");
        headerRow.createCell(13).setCellValue("车辆探伤有效期");
        headerRow.createCell(15).setCellValue("检修车辆长度：");


        // 从第四行开始填入longFactor值，并计算总和
        int rowNumber = 4; // 数据从第5行开始
        for (TbExcelVo vo : list) {
            Row row = sheet.createRow(rowNumber++);
            row.createCell(0).setCellValue(vo.getWorkerTeam());
            row.createCell(1).setCellValue(vo.getWorkGroup());
            row.createCell(2).setCellValue(vo.getLmtNumber());
            row.createCell(3).setCellValue(vo.getLmtType());
            row.createCell(4).setCellValue(vo.getRepairType());
            if (vo.getNeedDate() != null) {
                row.createCell(5).setCellValue(vo.getNeedDate());
            }
            if (vo.getRemark() != null) {
                row.createCell(7).setCellValue(vo.getRemark());
            }
            if (vo.getLongFactor() != null) {
                row.createCell(8).setCellValue(vo.getLongFactor());
            }
            // 对于日期，可能需要进行格式化

            Cell startDateCell = row.createCell(9);
            if (vo.getRepairStartDate() != null) {
                startDateCell.setCellValue(vo.getRepairStartDate());
                startDateCell.setCellStyle(dateStyle);
            }

            Cell endDateCell = row.createCell(10);
            if (vo.getRepairEndDate() != null) {
                endDateCell.setCellValue(vo.getRepairEndDate());
                endDateCell.setCellStyle(dateStyle);
            }
            if (vo.getPrompt() != null) {
                row.createCell(11).setCellValue(vo.getPrompt());
            }
            Cell cell11 = row.createCell(12);
            if (vo.getZdfRepairDate() != null) {

                cell11.setCellValue(vo.getZdfRepairDate());
                cell11.setCellStyle(dateStyle);
            }

            Cell cell12 = row.createCell(13);
            if (vo.getTsRepairDate() != null) {
                cell12.setCellValue(vo.getTsRepairDate());
                cell12.setCellStyle(dateStyle);
            }
        }


        // 调整列宽以适应内容
        // 设置所有行高
        for (Row row : sheet) {
            row.setHeightInPoints(20);
        }

// 设置所有列宽
        for (Sheet sh : workbook) {
            for (int i = 0; i < 300; i++) {
                sh.setColumnWidth(i, 14 * 256);
            }
        }


        OutputStream os = null;

        try {
            os = response.getOutputStream();
            workbook.write(os);
            os.flush();
            //os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
